﻿using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace DataShow.codeBLL
{
    public class SceneBLL
    {
        #region 搜索降雨量
        /// <summary>
        /// 搜索降雨量
        /// </summary>
        /// <param name="timeBegin"></param>
        /// <param name="timeEnd"></param>
        /// <param name="types"></param>
        /// <param name="stations"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="isByHand"></param>
        /// <returns></returns>
        public List<RainResult> SearchRain(DateTime timeBegin, DateTime timeEnd, List<Types> types, List<Station> stations, int pageSize, int pageIndex, bool isByHand)
        {
            List<RainResult> results = new List<RainResult>();
            RainResult item ;
            DataTable dt = new DataTable();
            string values = null;
            var typecondition = "";
            if (types.Count != 0)
            {

                foreach (var type in types)
                {
                    typecondition += ("'" + type + "',");
                }
                typecondition = typecondition.Substring(0, typecondition.Length - 1);
            }
            else
            {
                typecondition = "''";
            }
            string condition = "type in (" + typecondition + ") and CONVERT(datetime,(select substring([date],0,5)+'-'+substring([date],5,2)+'-'+substring([date],7,2))) between convert(datetime,'" + timeBegin.ToShortDateString() + "') and convert(datetime,'" + timeEnd.ToShortDateString() + "')";
            if (isByHand)
            {
                dt = new codeDAL.SceneProcess().SearchRain("AutoRainReal", pageSize, pageIndex, condition);
            }
            else
            {
                dt = new codeDAL.SceneProcess().SearchRain("AutoRainReal", pageSize, pageIndex, condition);
            }
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    values = row["value"].ToString();
                    string[] stationValues = values.Split('|');
                    if (stations != null)
                    {
                        foreach (var station in stations)
                        {
                            var nowstation = stationValues.Where(x => x.Contains(((long)station).ToString())).FirstOrDefault();
                            if (nowstation != null)
                            {
                                string value = nowstation.Split(';')[1];
                                item = new RainResult() { Time = row["date"].ToString(), Type = row["type"].ToString(), Station = station.ToString(), ShiKuang = value };
                                results.Add(item);
                            }
                        }
                    }
                }
            }
            return results;
        } 
        #endregion

        #region 计算降雨量总页数
        /// <summary>
        /// 计算降雨量总页数
        /// </summary>
        /// <param name="timeBegin"></param>
        /// <param name="timeEnd"></param>
        /// <param name="types"></param>
        /// <param name="isByHand"></param>
        /// <param name="stations"></param>
        /// <returns></returns>
        public int ClaculateRainCount(DateTime timeBegin, DateTime timeEnd, List<Types> types, bool isByHand, List<Station> stations)
        {
            List<RainResult> results = new List<RainResult>();
            DataTable dt = new DataTable();
            var typecondition = "";
            if (types.Count != 0)
            {

                foreach (var type in types)
                {
                    typecondition += ("'" + type + "',");
                }
                typecondition = typecondition.Substring(0, typecondition.Length - 1);
            }
            else
            {
                typecondition = "''";
            }
            string condition = "type in (" + typecondition + ") and CONVERT(datetime,(select substring([date],0,5)+'-'+substring([date],5,2)+'-'+substring([date],7,2))) between convert(datetime,'" + timeBegin.ToShortDateString() + "') and convert(datetime,'" + timeEnd.ToShortDateString() + "')";
            if (isByHand)
            {
                dt = new codeDAL.SceneProcess().CalculateCount("AutoRainReal", condition);
            }
            else
            {
                dt = new codeDAL.SceneProcess().CalculateCount("AutoRainReal", condition);
            }

            return dt.Rows.Count;
        } 
        #endregion

        #region 搜索温度
        /// <summary>
        /// 搜索温度
        /// </summary>
        /// <param name="timeBegin"></param>
        /// <param name="timeEnd"></param>
        /// <param name="types"></param>
        /// <param name="stations"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="isByHand"></param>
        /// <returns></returns>
        public List<TempResult> SearchTemp(DateTime timeBegin, DateTime timeEnd, List<TTypes> types, List<Station> stations, int pageSize, int pageIndex, bool isByHand)
        {
            List<TempResult> results = new List<TempResult>();
            TempResult item;
            DataTable dt = new DataTable();
            string values = null;
            var typecondition = "";
            if (types.Count != 0)
            {

                foreach (var type in types)
                {
                    typecondition += ("'" + type + "',");
                }
                typecondition = typecondition.Substring(0, typecondition.Length - 1);
            }
            else
            {
                typecondition = "''";
            }
            string condition = "type in (" + typecondition + ") and CONVERT(datetime,(select substring([date],0,5)+'-'+substring([date],5,2)+'-'+substring([date],7,2))) between convert(datetime,'" + timeBegin.ToShortDateString() + "') and convert(datetime,'" + timeEnd.ToShortDateString() + "')";
            if (isByHand)
            {
                dt = new codeDAL.SceneProcess().SearchTemp("AutoTempReal", pageSize, pageIndex, condition);
            }
            else
            {
                dt = new codeDAL.SceneProcess().SearchTemp("AutoTempReal", pageSize, pageIndex, condition);
            }
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    values = row["value"].ToString();
                    string[] stationValues = values.Split('|');
                    if (stations != null)
                    {
                        foreach (var station in stations)
                        {
                            var nowstation = stationValues.Where(x => x.Contains(((long)station).ToString())).FirstOrDefault();
                            if (nowstation != null)
                            {
                                string value = nowstation.Split(';')[1];
                                item = new TempResult() { Time = row["date"].ToString(), Type = row["type"].ToString(), Station = station.ToString(), Temp=value };
                                results.Add(item);
                            }
                        }
                    }
                }
            }
            return results;
        }
        #endregion

        #region 计算温度总页数
        /// <summary>
        /// 计算温度总页数
        /// </summary>
        /// <param name="timeBegin"></param>
        /// <param name="timeEnd"></param>
        /// <param name="types"></param>
        /// <param name="isByHand"></param>
        /// <param name="stations"></param>
        /// <returns></returns>
        public int ClaculateTempCount(DateTime timeBegin, DateTime timeEnd, List<TTypes> types, bool isByHand, List<Station> stations)
        {
            List<RainResult> results = new List<RainResult>();
            RainResult item;
            DataTable dt = new DataTable();
            string values = null;
            var typecondition = "";
            if (types.Count != 0)
            {

                foreach (var type in types)
                {
                    typecondition += ("'" + type + "',");
                }
                typecondition = typecondition.Substring(0, typecondition.Length - 1);
            }
            else
            {
                typecondition = "''";
            }
            string condition = "type in (" + typecondition + ") and CONVERT(datetime,(select substring([date],0,5)+'-'+substring([date],5,2)+'-'+substring([date],7,2))) between convert(datetime,'" + timeBegin.ToShortDateString() + "') and convert(datetime,'" + timeEnd.ToShortDateString() + "')";
            if (isByHand)
            {
                dt = new codeDAL.SceneProcess().CalculateCount("AutoTempReal", condition);
            }
            else
            {
                dt = new codeDAL.SceneProcess().CalculateCount("AutoTempReal", condition);
            }

            return dt.Rows.Count;
        }
        #endregion

    }
}